//
//  ViewController.m
//  LimitCharTextFiled
//
//  Created by Chengkai on 16/6/29.
//  Copyright © 2016年 com.zkingsoft. All rights reserved.
//

#import "ViewController.h"
#import "UITextField+Limit.h"


@interface ViewController ()

@end

#define kMaxLength 5

@implementation ViewController

#pragma mark -
#pragma mark Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [_lblTextFiled limitInputChars:10 callback:^{
//        NSLog(@"输入超过10个字符了啦");
//    }];
    
//    [_lblTextFiled limitInputLength:5 callback:^{
//        NSLog(@"输入长度满5");
//    }];
    
//    [_lblTextFiled limitOnlyNumber:4];
    
//    [_lblTextFiled limitOnlyNumberWithoutDot:4 callback:^{
//        NSLog(@"输入长度满了");
//    }];
    
    [_lblTextFiled limitOnlyNumber:4 callback:^{
        NSLog(@"输入长度满了");
    }];
    
//    [self setUP];
}

-(void)setUP{
    [_lblTextFiled addTarget:self action:@selector(textFiledEditChanged:) forControlEvents:UIControlEventEditingChanged];
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(textFiledEditChanged:)
//                                                name:@"UITextFieldTextDidChangeNotification"
//                                              object:nil];
}

#pragma mark -
#pragma mark Private Method


-(void)dealloc{
//    [[NSNotificationCenter defaultCenter]removeObserver:self
//                                                   name:@"UITextFieldTextDidChangeNotification"
//                                                 object:nil];
}

#pragma mark -
#pragma mark Event Response

-(void)textFiledEditChanged:(id)sender{
    
    UITextField *textField = _lblTextFiled;
    NSString *toBeString = textField.text;
    NSString *lang = _lblTextFiled.textInputMode.primaryLanguage; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > kMaxLength) {
                textField.text = [toBeString substringToIndex:kMaxLength];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > kMaxLength) {
            textField.text = [toBeString substringToIndex:kMaxLength];
        }
    }
}


#pragma mark -
#pragma mark Getter & Setter




#pragma mark -
#pragma mark PublicMethod




@end
